/*************************************************************************
 * File Name:    Longest_Substring_Without_Repeating_Characters.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/9 14:49:35
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Longest Substring Without Repeating Characters
 |
 | Given a string, find the length of the longest substring without repeating characters.
 | For example, the longest substring without repeating letters for "abcabcbb" is "abc",
 | which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s)
    {
        const int letter_num = 26;
        int N = s.size();
        vector<int> pre(letter_num, -1);
        
        int ans = 0;
        int last = -1;
        for (int i = 0; i < N; ++i) {
            last = max(last, pre[s[i] - 'a']);
            ans = max(ans, i - last);
            pre[s[i] - 'a'] = i;
        }
        return ans;
    }
};

